package com.cib.icarus.desensitize.wrapper.internals;

import com.cib.icarus.core.utils.ClassUtils;
import com.cib.icarus.desensitize.wrapper.DesAbstractWrapper;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GsonDesWrapper extends DesAbstractWrapper {

    private static final Logger logger = LoggerFactory.getLogger(GsonDesWrapper.class);

    private static final GsonDesWrapper instance = new GsonDesWrapper();

    private Gson gson = new Gson();

    private GsonDesWrapper() {
    }

    public static GsonDesWrapper getInstance() {
        return instance;
    }

    @Override
    public String showAsString(Object object) {
        try {
            return this.gson.toJson(desensitize(object));
        } catch (Exception ex) {
            logger.warn("try to write {}  desensitize json occur error {}!!!, will return toString", object, ex.getMessage(), ex);
            return object.toString();
        }

    }

    @Override
    public Object deepCopy(Object object) {
        return this.gson.fromJson(this.gson.toJson(object), object.getClass());
    }

}
